package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.SmsHomeAdvertise;
import com.itheima.mapper.SmsHomeAdvertiseMapper;
import com.itheima.service.db.SmsHomeAdvertiseService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeAdvertiseServiceImpl implements SmsHomeAdvertiseService {
    @Autowired
    SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    @Override
    public PageBeanVo findAdvertiseByPage(String endTime, String name, Integer pageNum, Integer pageSize, Integer type) {
        //分页构造器
        Page<SmsHomeAdvertise> page = new Page<>(pageNum,pageSize);

        //条件构造器
        LambdaQueryWrapper<SmsHomeAdvertise> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name),SmsHomeAdvertise::getName,name)
                .eq(type != null,SmsHomeAdvertise::getType,type)
                .eq(StrUtil.isNotEmpty(endTime),SmsHomeAdvertise::getEndTime,endTime);

        //通过mapper获取分页数据
        Page<SmsHomeAdvertise> advertisePage = smsHomeAdvertiseMapper.selectPage(page, qw);

        return new PageBeanVo(pageNum,pageSize, advertisePage.getTotal(), advertisePage.getRecords());
    }

    @Override
    public SmsHomeAdvertise findAdvertiseInfo(Integer id) {
        //通过mapper查询对象
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseMapper.selectById(id);
        //返回
        return smsHomeAdvertise;
    }

    @Override
    public void updateAdvertise(SmsHomeAdvertise advertise, Integer id) {
        //通过mapper保存对象
        smsHomeAdvertiseMapper.updateById(advertise);
    }

    @Override
    public void updateAdvertiseStatus(Integer id, Integer status) {
        //通过mapper根据id查询对象
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseMapper.selectById(id);
        smsHomeAdvertise.setStatus(status);
    }

    @Override
    public void addAdvertise(SmsHomeAdvertise advertise) {
        //通过mapper添加对象
        smsHomeAdvertiseMapper.insert(advertise);
    }

    @Override
    public void deleteAdvertise(List<Integer> ids) {
        //通过mapper删除对象
        for (Integer id : ids) {
            smsHomeAdvertiseMapper.deleteById(id);
        }
    }
}
